<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides global methods, that can be used for accessing data in database layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

abstract class ADS_DefaultModel {
    
    //
    const NO_STATEMENT    =  0;
    const USE_STATEMENT   =  1;
    const RESET_STATEMENT =  2;      
    //
    protected $offset   = 0;
    protected $limit    = 20;
    protected $orderBy  = array();
    //
    protected $useLimit   = self::NO_STATEMENT; 
    protected $useFilter  = self::NO_STATEMENT; 
    protected $useOrderBy = self::NO_STATEMENT; 
 
    public function resetOrderBy() {
        $this->orderBy    = array();
        $this->useOrderBy = self::NO_STATEMENT;
    }

    public function resetLimit() {
        $this->offset   = 0;
        $this->limit    = 20;
        $this->useLimit = self::NO_STATEMENT;
    }
    
    public function resetFilter() {
        $this->filter    = array();
        $this->useFilter = self::NO_STATEMENT;
    }
    
    public function setPager($page, $limit = 20){
        if(!is_int($page) || !is_int($limit)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        $this->offset   = ($page-1)*$limit;
        $this->limit    = $limit;
        $this->useLimit = self::USE_STATEMENT;
        
        return $this;
    }
    
    public function setFilter($filter){
        if(!is_array($filter)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        $this->filter    = $filter;
        $this->useFilter = self::USE_STATEMENT;
        
        return $this;
    }
    
    public function setSorter($orderBy){
        if(!is_array($orderBy)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        if(count($orderBy) > 0){
            $this->orderBy = $orderBy;
            $this->useOrderBy = self::USE_STATEMENT;
        }        
        
        return $this;
    }
    
    public function getLimit() {
        return $this->limit;
    }
    
}
